package com.yipao.db.domain.goods;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * 商品基本信息
 *
 * @author y.x
 */
@Data
@EqualsAndHashCode
@ToString
public class Goods {

    /**
     * 商品下架状态
     */
    public static final Integer GOODS_OUT_STATUS = 0;

    /**
     * 商品上架状态
     */
    public static final Integer GOODS_IN_STATUS = 1;

    private Integer id;

    /**
     * 商品编号
     */
    private String goodsSn;

    private String name;
    /**
     * 分类id
     */
    private Integer categoryId;
    /**
     * 品牌id
     */
    private Integer brandId;

    private String[] gallery;

    private String keywords;

    private String brief;

    /**
     * 是否上架
     * 1 上架
     * 0 下架
     */
    private Integer isOnSale;

    private Short sortOrder;

    /**
     * 商品页面商品图片
     */
    private String picUrl;

    /**
     * 商品分享朋友圈图片
     */
    private String shareUrl;

    /**
     * 是否新品首发，如果设置则可以在新品首发页面展示
     */
    private Boolean isNew;

    private Boolean isHot;

    private String unit;

    /***
     * 商品价格
     */
    private BigDecimal counterPrice;

    /**
     * 零售价
     */
    private BigDecimal retailPrice;

    private LocalDateTime addTime;

    private LocalDateTime updateTime;

    private Integer deleted;

    private String detail;

    /**
     * 标签
     */
    private String tags;
    /**
     * 配方id
     */
    private Integer formulaId;


    public enum Column {
        id("id", "id", "INTEGER", false),
        goodsSn("goods_sn", "goodsSn", "VARCHAR", false),
        name("name", "name", "VARCHAR", true),
        categoryId("category_id", "categoryId", "INTEGER", false),
        brandId("brand_id", "brandId", "INTEGER", false),
        gallery("gallery", "gallery", "VARCHAR", false),
        keywords("keywords", "keywords", "VARCHAR", false),
        brief("brief", "brief", "VARCHAR", false),
        isOnSale("is_on_sale", "isOnSale", "BIT", false),
        sortOrder("sort_order", "sortOrder", "SMALLINT", false),
        picUrl("pic_url", "picUrl", "VARCHAR", false),
        shareUrl("share_url", "shareUrl", "VARCHAR", false),
        isNew("is_new", "isNew", "BIT", false),
        isHot("is_hot", "isHot", "BIT", false),
        unit("unit", "unit", "VARCHAR", false),
        counterPrice("counter_price", "counterPrice", "DECIMAL", false),
        retailPrice("retail_price", "retailPrice", "DECIMAL", false),
        addTime("add_time", "addTime", "TIMESTAMP", false),
        formulaId("formula_id", "formulaId", "VARCHAR", false),
        updateTime("update_time", "updateTime", "TIMESTAMP", false),
        deleted("deleted", "deleted", "BIT", false),
        detail("detail", "detail", "LONGVARCHAR", false),
        tags("tags", "tags", "VARCHAR", false);

        private static final String BEGINNING_DELIMITER = "`";

        private static final String ENDING_DELIMITER = "`";

        private final String column;

        private final boolean isColumnNameDelimited;

        private final String javaProperty;

        private final String jdbcType;


        public String value() {
            return this.column;
        }


        public String getValue() {
            return this.column;
        }


        public String getJavaProperty() {
            return this.javaProperty;
        }


        public String getJdbcType() {
            return this.jdbcType;
        }


        Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
            this.column = column;
            this.javaProperty = javaProperty;
            this.jdbcType = jdbcType;
            this.isColumnNameDelimited = isColumnNameDelimited;
        }


        public String desc() {
            return this.getEscapedColumnName() + " DESC";
        }


        public String asc() {
            return this.getEscapedColumnName() + " ASC";
        }


        public static Column[] excludes(Column... excludes) {
            ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
            if (excludes != null && excludes.length > 0) {
                columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
            }
            return columns.toArray(new Column[]{});
        }


        public String getEscapedColumnName() {
            if (this.isColumnNameDelimited) {
                return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
            } else {
                return this.column;
            }
        }


        public String getAliasedEscapedColumnName() {
            return this.getEscapedColumnName();
        }
    }
}
